package com.idroid.utils;

import android.content.Context;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.util.Log;
import android.widget.TextView;

public class TextColorUtil {
    private static final String TAG_COLOR = "setColorSpan() Error:";
    private static final String ERROR_TIPS = "Please check the following error:\nTextView is null ?\nTextView.getText() is null ?\n" +
            "startIndex/endIndex is out of bounds ?";

    public static void setTextColor(Context ctx, TextView textView, String str, String startTag, String endTag, int color) {
        ForegroundColorSpan colorSpan = new ForegroundColorSpan(ctx.getResources().getColor(color));
        SpannableStringBuilder builder = new SpannableStringBuilder(str);
        int startIndex = str.indexOf(startTag);
        int endIndex = str.length();
        if (!TextUtils.isEmpty(endTag)) {
            endIndex = str.lastIndexOf(endTag);
        }
        if (startIndex > 0) {
            builder.setSpan(colorSpan, startIndex, endIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            textView.setText(builder);
        }
    }

    public static void setTextColor(TextView textView, String str, String startTag, String endTag, int color) {
        ForegroundColorSpan colorSpan = new ForegroundColorSpan(color);
        SpannableStringBuilder builder = new SpannableStringBuilder(str);
        int startIndex = str.indexOf(startTag);
        int endIndex = str.length();
        if (!TextUtils.isEmpty(endTag)) {
            endIndex = str.lastIndexOf(endTag);
        }
        if (startIndex > 0) {
            builder.setSpan(colorSpan, startIndex, endIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            textView.setText(builder);
        }
    }

    public static void setTextColor(TextView textView, String str, String startTag, int color) {
        ForegroundColorSpan colorSpan = new ForegroundColorSpan(color);
        SpannableStringBuilder builder = new SpannableStringBuilder(str);
        int startIndex = str.indexOf(startTag);
        int endIndex = str.length();
        builder.setSpan(colorSpan, startIndex, endIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        textView.setText(builder);
    }

    public static void setTextColor(Context ctx, TextView textView, String str, String startTag, int color) {
        ForegroundColorSpan colorSpan = new ForegroundColorSpan(ctx.getResources().getColor(color));
        SpannableStringBuilder builder = new SpannableStringBuilder(str);
        int startIndex = str.indexOf(startTag);
        int endIndex = str.length();
        builder.setSpan(colorSpan, startIndex, endIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        textView.setText(builder);
    }

    public static void setTextColor(TextView textView, String str, int startIndex, int color) {
        ForegroundColorSpan colorSpan = new ForegroundColorSpan(color);
        SpannableStringBuilder builder = new SpannableStringBuilder(str);
        builder.setSpan(colorSpan, startIndex, str.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        textView.setText(builder);
    }

    public static void setTextSize(TextView textView, String str, String startFlag, String endFlag) {
        setTextSize(textView, str, startFlag, endFlag, 1.35f);
    }

    public static void setTextSize(TextView textView, String str, String startFlag, String endFlag, float size) {
        SpannableString msp = new SpannableString(str);
        msp.setSpan(new RelativeSizeSpan(size), str.indexOf(startFlag) + 1, str.indexOf(endFlag) + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        textView.setText(msp);
    }

    //设置字体中指定位置内的字符串颜色（如"成功到账100元"中的100颜色的改变）
    public static void setColorSpan(Context ctx,TextView tv, int startIndex, int endIndex, int color) {
        if (null != tv && !TextUtils.isEmpty(tv.getText()) && tv.getText().length() > startIndex && tv.getText().length() >=
                endIndex) {
            ForegroundColorSpan span = new ForegroundColorSpan(ctx.getResources().getColor(color));
            SpannableString spanString = new SpannableString(tv.getText());
            spanString.setSpan(span, startIndex, endIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            tv.setText(spanString);
        } else {
            Log.w(TAG_COLOR, ERROR_TIPS);
        }
    }
}
